﻿using DDD.Core.CQRS;
using DDD.Core.Result;
using DDD.Domain.Abstract.UnitOfWork;
using DDD.Domain.DomainManagers;
using DDD.Domain.Repositories;

namespace DDD.Application.Commands.UserCommand.CreateUser
{
    internal class CreateUserCommandHandler : ICommandHandler<CreateUserCommand, ApiResult<Guid>, Guid>
    {
        private readonly IUserRepository _userRepository;
        private readonly UserManager _userManager;
        private readonly IUnitOfWork _unitOfWork;

        public CreateUserCommandHandler(IUserRepository userRepository, UserManager userManager, IUnitOfWork unitOfWork)
        {
            _userRepository = userRepository;
            _userManager = userManager;
            _unitOfWork = unitOfWork;
        }

        public async Task<ApiResult<Guid>> Handle(CreateUserCommand request, CancellationToken cancellationToken)
        {
            var result = await _userRepository.AddAsync(request.ToUser());
            await _unitOfWork.SaveChanegeAsync();

            return ApiResult<Guid>.Success(result.Id);
        }
    }
}
